package cn.iocoder.yudao.module.vehicle.service.fuelcardapplication;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.vehicle.enums.StatuesEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.vehicle.controller.admin.fuelcardapplication.vo.*;
import cn.iocoder.yudao.module.vehicle.dal.dataobject.fuelcardapplication.FuelcardApplicationDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.hutool.core.util.ObjUtil;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.vehicle.dal.mysql.fuelcardapplication.FuelcardApplicationMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.vehicle.enums.ErrorCodeConstants.*;
import cn.iocoder.yudao.module.bpm.api.task.BpmProcessInstanceApi;
import cn.iocoder.yudao.module.bpm.api.task.dto.BpmProcessInstanceCreateReqDTO;
import com.mzt.logapi.context.LogRecordContext;

/**
 * 油卡申请记录 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FuelcardApplicationServiceImpl implements FuelcardApplicationService {

    @Resource
    private FuelcardApplicationMapper fuelcardApplicationMapper;

    @Resource
    private BpmProcessInstanceApi bpmProcessInstanceApi;

    /**
     * vehicle 油卡申请审批流程标识
     */
    public static final String BPM_PROCESS_DEFINITION_KEY = "vehicle-application-audit";

    @Override
    public Long createFuelcardApplication(FuelcardApplicationSaveReqVO createReqVO) {
        // 插入
        FuelcardApplicationDO fuelcardApplication = BeanUtils.toBean(createReqVO, FuelcardApplicationDO.class);
        fuelcardApplicationMapper.insert(fuelcardApplication);

        // 返回
        return fuelcardApplication.getId();
    }

    @Override
    public void updateFuelcardApplication(FuelcardApplicationSaveReqVO updateReqVO) {
        // 校验存在
        validateFuelcardApplicationExists(updateReqVO.getId());
        // 更新
        FuelcardApplicationDO updateObj = BeanUtils.toBean(updateReqVO, FuelcardApplicationDO.class);
        fuelcardApplicationMapper.updateById(updateObj);
    }

    @Override
    public void deleteFuelcardApplication(Long id) {
        // 校验存在
        validateFuelcardApplicationExists(id);
        // 删除
        fuelcardApplicationMapper.deleteById(id);
    }

    @Override
        public void deleteFuelcardApplicationListByIds(List<Long> ids) {
        // 删除
        fuelcardApplicationMapper.deleteByIds(ids);
        }



    @Override
    public FuelcardApplicationDO getFuelcardApplication(Long id) {
        return fuelcardApplicationMapper.selectById(id);
    }


    @Override
    public FuelcardApplicationDO getFuelcardApplicationByProcId(String id) {
        return fuelcardApplicationMapper.selectByProcId(id);
    }

    @Override
    public void submitApplicaton(Long id, Long userId){
        //1. 校验油卡申请是否在审批
        FuelcardApplicationDO fuelcardApplication = validateFuelcardApplicationExists(id);
        if (ObjUtil.notEqual(fuelcardApplication.getAuditStatus(), StatuesEnum.DRAFT.getStatus())) {
            throw exception(FUELCARD_APPLICATION_NOT_DRAFT);
        }
// 2. 创建申请审批流程实例
        String processInstanceId = bpmProcessInstanceApi.createProcessInstance(userId, new BpmProcessInstanceCreateReqDTO()
                .setProcessDefinitionKey(BPM_PROCESS_DEFINITION_KEY).setBusinessKey(String.valueOf(id)));
        // 3. 更新工作流编号
        fuelcardApplicationMapper.updateById(new FuelcardApplicationDO().setId(id).setProcessInstanceId(processInstanceId)
                .setAuditStatus(StatuesEnum.PROCESS.getStatus()));
        // 3. 记录日志
        LogRecordContext.putVariable("contractName", fuelcardApplication.getApplicant());
    }

    @Override
    public PageResult<FuelcardApplicationDO> getFuelcardApplicationPage(FuelcardApplicationPageReqVO pageReqVO) {
        return fuelcardApplicationMapper.selectPage(pageReqVO);
    }

    private FuelcardApplicationDO validateFuelcardApplicationExists(Long id) {
        FuelcardApplicationDO application = fuelcardApplicationMapper.selectById(id);
        if (application == null) {
            throw exception(FUELCARD_APPLICATION_NOT_EXISTS);
        }
        return application;
    }

}